package main

import (
	"fmt"
	"log"
	"math/big"
	"os"

	"github.com/ethereum/go-ethereum/accounts/abi/bind"

	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/ethclient"
	"github.com/ethereum/go-ethereum/rpc"
)

func newAccount(pass string) {
	//建立连接
	cli, err := rpc.Dial("http://localhost:8545")
	if err != nil {
		log.Fatal("failed to connect to geth~!", err)
	}
	defer cli.Close()
	//调用创建账户personal.newAccount
	var account string
	cli.Call(&account, "personal_newAccount", pass)
	if err != nil {
		log.Fatal("failed to create account", err)
	}
	fmt.Println("new acc", account)
}
func main() {
	//newAccount("liuyue")

	//addPerson("liuyue12345", 18)
	//getPerson(3)
	getBalance("0x94d5d6d9d4c6b0bfbdb7e62a81aee6005147d1cc")
	//transferamount("0x94d5d6d9d4c6b0bfbdb7e62a81aee6005147d1cc", 299)
	//getBalance("0x94d5d6d9d4c6b0bfbdb7e62a81aee6005147d1cc")
}

func addPerson(_name string, age int64) {
	//1.dial
	cli, err := ethclient.Dial("http://localhost:8545")

	if err != nil {
		log.Fatal("failed to connect to geth", err)
	}
	//2.找到函数调用入口
	inst, err := NewCallabi(common.HexToAddress("0xc610bc9f5ccdeb59e5fe4d3b7bfdc9cd234f9b31"), cli)
	if err != nil {
		log.Fatal("failed to connect to NewCallabi", err)
	}
	//3.设置签名 需要owner的keystore文件
	file, err := os.Open("/Users/liuyue/ethdata/data/keystore/UTC--2018-11-16T06-00-56.254882000Z--7f303c13d542e96c264d47ddf771a9e75497e9b8")
	if err != nil {
		log.Fatal("failed to open KeyStore File", err)
	}
	auth, err := bind.NewTransactor(file, "liuyue")
	if err != nil {
		log.Fatal("failed to newTransactor:", err)
	}
	//instance
	inst.AddPerson(auth, _name, big.NewInt(age))

}

func getPerson(tokenId int64) {
	//1.dial
	cli, err := ethclient.Dial("http://localhost:8545")

	if err != nil {
		log.Fatal("failed to connect to geth", err)
	}
	//2.找到函数调用入口
	inst, err := NewCallabi(common.HexToAddress("0xc610bc9f5ccdeb59e5fe4d3b7bfdc9cd234f9b31"), cli)
	if err != nil {
		log.Fatal("failed to connect to NewCallabi", err)
	}
	//3.调用
	name, err := inst.GetPerson(nil, big.NewInt(tokenId))
	if err != nil {
		log.Fatal("failed to invoke GetPerson", err)
	}
	fmt.Println(name)
}

func getBalance(addstr string) {
	//1.dial
	cli, err := ethclient.Dial("http://localhost:8545")

	if err != nil {
		log.Fatal("failed to connect to geth", err)
	}

	instance, err := NewCallkccabi(common.HexToAddress("0xd3838d1e4dced48c9a19195618fe544c1f12e4b8"), cli)

	if err != nil {
		log.Fatal("failed to connect to NewCallkccabi", err)
	}

	name, err := instance.BalanceOf(nil, common.HexToAddress(addstr))
	if err != nil {
		log.Fatal("failed to invoke BalanceOf method")
	}
	fmt.Println(name)

}

func transferamount(_to string, _value int64) {
	//1.dial
	cli, err := ethclient.Dial("http://localhost:8545")

	if err != nil {
		log.Fatal("failed to connect to geth", err)
	}
	//2.找到函数调用入口
	inst, err := NewCallkccabi(common.HexToAddress("0xd3838d1e4dced48c9a19195618fe544c1f12e4b8"), cli)
	if err != nil {
		log.Fatal("failed to connect to NewCallabi", err)
	}
	//3.设置签名 需要owner的keystore文件
	file, err := os.Open("/Users/liuyue/ethdata/data/keystore/UTC--2018-11-16T06-00-56.254882000Z--7f303c13d542e96c264d47ddf771a9e75497e9b8")
	if err != nil {
		log.Fatal("failed to open KeyStore File", err)
	}
	auth, err := bind.NewTransactor(file, "liuyue")
	if err != nil {
		log.Fatal("failed to newTransactor:", err)
	}

	inst.Transfer(auth, common.HexToAddress(_to), big.NewInt(_value))
}
